from Position import Position

class ClickEntity:

    # position = []

    def __init__(self, querycode):
        self.querycode = querycode
        self.position = []

    def doRank(self):
        # 按照位置从小往大排
        self.position.sort(key=lambda pos: pos.position)
        # 从右往左遍历，当前元素position[i].click=1 同时前面的元素position[i-1].click=0,交换位置
        for i, pos in enumerate(self.position):
            if i == 0:
                continue
            if pos.click == 1 and self.position[i - 1].click == 0:
                # 互换位置
                self.position[i] = self.position[i - 1]
                self.position[i - 1] = pos

    def __str__(self):
        return 'querycode {},position {}'.format(self.querycode,self.position)


if __name__ == '__main__':
    print('hello word')
    list = [Position('d', 4, 0), Position('b', 2, 0), Position('a', 1, 0), Position('c', 3, 1), Position('e', 5, 1)]
    entity = ClickEntity("querycode")
    entity.position = list
    entity.doRank()
    print(entity)
    for pos in entity.position:
        print(pos)
